import json

from fastapi import Request, APIRouter, Body, Query
from datetime import datetime, timedelta
from core.Auth import decode_access_token
from core.Response import success, fail
from core.Utils import process_payment_request, random_str
from models.models import SiteInfo, User, User_vip, Post, PostDteta
from schemas.posts import SiteInfo_Pydantic, User_Pydantic, Post_Pydantic, AdminPost, admin_Post_Pydantic

router = APIRouter(prefix='')


@router.get('')
async def search(request: Request,
                 title: str = Query(None)):
    print(title)

    search_result = await Post.filter(title__icontains=title).all()
    serialized_items = []
    for item in search_result:
        post = admin_Post_Pydantic.from_orm(item).dict()
        serialized_items.append(post)

    return success(data=serialized_items, msg='搜索成功')
